package org.testis.gui.mvc.controller.testdesign;

import org.testis.gui.mvc.controller.*;
import org.testis.gui.mvc.model.testdesign.LoadTestSuiteModel;
import org.testis.gui.mvc.view.testdesign.LoadTestSuiteView;
import org.testis.utils.SortAlphabetically;
import java.util.HashMap;

import org.testis.utils.middleware.TestDesign;


/**
 *
 * @author Cahit Emir Erel
 */
public class LoadTestSuiteController extends AbstractController
{

    public static final String SELECT_SUITE = "Suite";
    public static final String SHOW_LOADER = "LoaderVisibility";
    public static final String SHOW_DESCRIPTION = "SuiteDescription";


    public LoadTestSuiteController()
    {
        super();
    }


    public void startLoader()
    {
        initialize();
        setModelProperty(SHOW_LOADER,
                         true);
    }


    public void initialize()
    {
        LoadTestSuiteModel loadTestSuiteModel = new LoadTestSuiteModel();
        LoadTestSuiteView loadTestSuiteView = new LoadTestSuiteView(
            TestDesignControllerList.newInstance().getParentFrame(),
            true);
        super.addModel(loadTestSuiteModel);
        super.addView(loadTestSuiteView);
        loadTestSuiteView.pack();
    }


    public void operationLoad()
    {
        String result = (String) getModelProperty(SELECT_SUITE);
        if (result != null && !result.equals("")) {

            TestDesignControllerList.newInstance().getTreeController().
                operationLoadTreeFromDb(result);
        }
    }


    public void operationSelectTestSuite(String name)
    {
        setModelProperty(SELECT_SUITE,
                         name);
        String result;
        if (m_hashMap.containsKey(name)) {
            result = m_hashMap.get(name);
        }
        else {
            result = getTestSuiteDetails(name);
            m_hashMap.put(name,
                   result);
        }
        setModelProperty(SHOW_DESCRIPTION,
                         result);
    }
    private HashMap<String, String> m_hashMap;


    private String getTestSuiteDetails(String name)
    {
        return TestDesign.newInstance().getTestSuiteDetails(name);
    }


    public String[] getTestSuiteNames()
    {
        String[] temp = TestDesign.newInstance().getTestSuiteNames();
        m_hashMap = new HashMap<String, String>(temp.length);
        return SortAlphabetically.sortAlphabetically(temp,false);
    }

    
    public void operationCloseWindow()
    {
        this.cleanAll();
    }
}
